﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Manatee.Trello;

namespace AIC_Notification
{
    class TrelloProcessorSOF: TrelloProcessorAIC
    {
        // Enums and Constants
        enum ApplicationLists { CEF, SL, PS, HAL, RL, T, SS, LI, CF };
        static string[] ApplicationListsName = new string[] { "CEF", "SL", "PS", "HAL", "RL", "T", "SS", "LI", "CF" };
        enum ApplicationListType { EssayList, TaskList, SchoolList };

        // Rex Patterns
        private static string ApplicationSchoolListPattern = @"S(-R(?<Rally>\d+))?(?<Type>(?i:-(E|E2)))?(?<NoSuppliment>(?i:-NS))?\s*[:：]\s*(?<SchoolName>[\w\s-.&]+)";
        private static string CEFCardPattern = @"(?<Number>(?i:CE[FR]))(\s*[/-]?\s*(?i:D)(?<Draft>\d+))?(\s*[/-]?\s*(?i:R)(?<Revision>\d+))?(?<Final>[/-]\s*(?i:F))?";
        private static string PSCardPattern = @"(?i:T)(?<Number>\d+)\s*[/-]?\s*(?i:D)(?<Draft>\d+)(\s*[/-]?\s*(?i:R)(?<Revision>\d+))?(?<Final>[/-]\s*(?i:F))?";
        private static string HALCardPattern = @"(?<Number>(?i:MAJ))?\s*[/-]?\s*(?i:D)(?<Draft>\d+)(\s*[/-]?\s*(?i:R)(?<Revision>\d+))?(?<Final>[/-]\s*(?i:F))?";
        private static string SupplimentEssayCardPattern = @"(?i:E)(?<Number>\d+)\s*[/-]?\s*(?<Brainstorm>(?i:BS))?\s*[/-]?\s*(?i:D)(?<Draft>\d+)(\s*[/-]?\s*(?i:R)(?<Revision>\d+))?(?<Final>[/-]\s*(?i:F))?";

        public TrelloProcessorSOF()
        {
            Program = "SOF";
            CacheFileName = String.Format("AIC_Notification-{0}-Cache.json", Program);
            TempCacheFileName = String.Format("AIC_Notification-{0}-Cache-Temp.json", Program);
            BoardNamePattern = @"^(?<ApplicationYear>\d{4})-\d{2}\s+(?<Program>[\w-]+)\s+(?<City>\w{2})\s+(?<StudentName>\w[\w\s]+\w)\s*[,，]\s*(?<AcademicConsultant1>\w[\w\s]+\w)\s*([&-]\s*(?<AcademicConsultant2>\w[\w\s]*\w)\s*)?[,，]\s*(?<EducationConsultant>\w[\w\s]+\w)";
        }

        public override void SetUpdate(ref Student ApplyingStudent, bool HasUpdate)
        {
            ApplyingStudent.HasSOFUpdate = HasUpdate;
        }

        public override bool GetUpdate(Student ApplyingStudent)
        {
            return ApplyingStudent.HasSOFUpdate;
        }

        public override void ReadBoard(Student ApplyingStudent)
        {
            Manatee.Trello.Board board = new Manatee.Trello.Board(ApplyingStudent.TrelloId); ;
            SetProcessingContext(board);

            SOF_Application application = null;

            if (Global.SOF_Applications.TryGetValue(ApplyingStudent.TrelloId, out application))
            {
                if (application != null)
                {
                    application.Reset();
                    Global.SysUtil.Log(String.Format("Upddating Existing Board: {0}", ApplyingStudent.Name), 1);
                }
            }

            if (application == null)
            {
                Global.SysUtil.Log(String.Format("Creating New Board: {0}", ApplyingStudent.Name), 1);
                application = new SOF_Application(board.Name, board.Id, board.Url,
                                                  ApplyingStudent.ApplicationYear,
                                                  ApplyingStudent.Program,
                                                  ApplyingStudent.GetCity(),
                                                  ApplyingStudent.Name,
                                                  ApplyingStudent.PT,
                                                  ApplyingStudent.ST,
                                                  ApplyingStudent.EC);
                Global.SOF_Applications.Add(board.Id, application);
            }

            ReadLists(board, ref application);
        }

        public override void AddStudentToLists()
        {
            List<string> StudentsToRemove = new List<string>();

            foreach (var student in Global.SOF_Applications)
            {
                AIC_Application Application = student.Value;

                // Validate if application is still valid
                if (MatchBoardWithStudent(ref Application, Global.SOF_StudentDB.Students))
                {
                    // Build Teacher List (PT, ST, EC)
                    AddStudentToTeacher(student.Value.AC_PT, student.Value);
                    if (student.Value.AC_ST != "")
                        AddStudentToTeacher(student.Value.AC_ST, student.Value);
                    AddStudentToTeacher(student.Value.EC, student.Value);

                    // Build City List
                    AddStudentToCity(student.Value);
                }
                else
                {
                    StudentsToRemove.Add(student.Key);
                    Global.SysUtil.Log(String.Format("Removing Board: {0}", student.Value.BoardName), 2);
                }
            }

            // Remove outdated records
            foreach (var StudentKey in StudentsToRemove)
                Global.SOF_Applications.Remove(StudentKey);
        }

        public void AddStudentToCity(SOF_Application student)
        {
            City TargetCity = CityDB.GetCityByName(student.City);

            if (TargetCity != null)
                TargetCity.Students.Add(student);
            else
                Global.SysUtil.Log(String.Format("Unable to insert \"{0}\" to City list: {1}", student.Name, student.City), 2);
        }

        public override void AddStudentToTeacher(string TeacherName, AIC_Application student)
        {
            Teacher teacher;
            AIC_Application hasStudent;

            teacher = TeacherDB.GetTeacherByName(TeacherName);
            if (teacher != null)
            {
                // Check if duplicate, add only if student not already in board
                hasStudent = teacher.Students.SingleOrDefault(AIC_Application => AIC_Application.Name.Equals(student.Name));
                if (hasStudent == null)
                {
                    teacher.Students.Add((SOF_Application)student);
                    Global.SysUtil.Log(String.Format("Added Student: {0} to Teacher: {1}", student.Name, teacher.Name));
                }
            }
            else
            {
                Global.SysUtil.Log(String.Format("Teacher not found: {0} for student {1}", TeacherName, student.Name), 2);
            }
        }

        public void ReadLists(Manatee.Trello.Board board, ref SOF_Application student)
        {
            SOF_WorkList ApplicationList = null;
            ApplicationListType type = ApplicationListType.EssayList;

            bool ValidList = false;
            int TotalSpecializedLists = Enum.GetNames(typeof(ApplicationLists)).Length;

            // Initialize Specialized List Counter
            int[] HasSpecializedLists = new int[TotalSpecializedLists];
            for (int i = 0; i < TotalSpecializedLists; i++)
                HasSpecializedLists[i] = 0;

            // Setup parser
            Regex ASListRegex = new Regex(ApplicationSchoolListPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));

            foreach (var list in board.Lists)
            {
                SetProcessingContext(list);

                // Try to validate list name

                string ListName = list.Name;

                if (!ValidList && ListName.StartsWith("CEF"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "CEF", "CEF", 0);
                    type = ApplicationListType.EssayList;
                    HasSpecializedLists[(int)ApplicationLists.CEF] += 1;
                    ValidList = true;
                }

                if (!ValidList && ListName.StartsWith("SL:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "SL", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.SL] += 1;
                    ValidList = true;
                }

                if (!ValidList && ListName.StartsWith("PS-R1"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "PS", "PS", 1);
                    type = ApplicationListType.EssayList;
                    HasSpecializedLists[(int)ApplicationLists.PS] += 1;
                    ValidList = true;
                }

                if (!ValidList && ListName.StartsWith("HAL-R1"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "HAL", "HAL", 1);
                    type = ApplicationListType.EssayList;
                    HasSpecializedLists[(int)ApplicationLists.HAL] += 1;
                    ValidList = true;
                }

                if (!ValidList && ListName.StartsWith("RL:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "RL", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.RL] += 1;
                    ValidList = true;
                }

                if (!ValidList && ListName.StartsWith("T:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "T", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.T] += 1;
                    ValidList = true;
                }

                if (!ValidList && ListName.StartsWith("SS:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "SS", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.SS] += 1;
                    ValidList = true;
                }

                if (!ValidList && ListName.StartsWith("LI:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "LI", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.LI] += 1;
                    ValidList = true;
                }
                /* Not using AF in 2018-19
                if (!ValidList && ListName.StartsWith("AF:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "AF", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.AF] += 1;
                    ValidList = true;
                }
                */
                if (!ValidList && ListName.StartsWith("CF:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "CF", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.CF] += 1;
                    ValidList = true;
                }
                /* Not using V in 2018-19
                if (!ValidList && ListName.StartsWith("V:"))
                {
                    ApplicationList = new SOF_WorkList(list.Id);
                    ApplicationList.SetList(ListName, "V", "", -1);
                    type = ApplicationListType.TaskList;
                    HasSpecializedLists[(int)ApplicationLists.V] += 1;
                    ValidList = true;
                }
                */
                if (!ValidList && ListName.StartsWith("S-R")) // A School?
                {
                    Match m = ASListRegex.Match(ListName);
                    if (m.Success)
                    {
                        int Rally = -1;
                        SOF_WorkList.ApplicationSchoolType Type = SOF_WorkList.ApplicationSchoolType.None;
                        bool NoSuppliment = false;

                        string SchoolName = m.Groups["SchoolName"].Value.Trim();

                        if (m.Groups["Rally"].Value != "")
                            Rally = Int32.Parse(m.Groups["Rally"].Value);

                        if (m.Groups["Type"].Value.Equals("-E", StringComparison.CurrentCultureIgnoreCase))
                        {
                            Type = SOF_WorkList.ApplicationSchoolType.Early1;
                        }
                        else if (m.Groups["Type"].Value.Equals("-E2", StringComparison.CurrentCultureIgnoreCase))
                        {
                            Type = SOF_WorkList.ApplicationSchoolType.Early2;
                        }

                        if (m.Groups["NoSuppliment"].Value.Equals("-NS", StringComparison.CurrentCultureIgnoreCase))
                        {
                            NoSuppliment = true;
                        }

                        ApplicationList = new SOF_WorkList(list.Id);
                        ApplicationList.SetList(ListName, "School", SchoolName, Rally, Type, NoSuppliment);
                        type = ApplicationListType.SchoolList;
                    }
                    else
                    {
                        if (!ListName.StartsWith("S-R#"))
                            Global.InputErrors.Add(new Error(student, null, null, Error.Owner.ac, String.Format("Likely a misformatted School list ({0})", ListName)));
                    }
                    ValidList = m.Success;
                }

                if (ValidList && (ApplicationList != null))
                {
                    Global.SysUtil.Log("Process List: " + ProcessingList.Name);

                    student.AddSchool(ApplicationList, ApplicationList.Rally);
                    if (type == ApplicationListType.EssayList)          // Should be reading essay cards
                    {
                        ReadEssayCards(list, student, ref ApplicationList);
                        student.NoOfEssayList += 1;
                    }
                    else if (type == ApplicationListType.TaskList)
                    {
                        if (Global.ProcessTaskList)
                            ReadTaskCards(list, student, ref ApplicationList);   // Should be reading task cards
                        student.NoOfTaskList += 1;
                    }
                    else
                    {
                        ReadEssayCards(list, student, ref ApplicationList);
                        student.NoOfEssayList += 1;
                        if (Global.ProcessTaskList)
                            ReadTaskCards(list, student, ref ApplicationList);
                        student.NoOfTaskList += 1;
                    }
                    ApplicationList = null;
                    ValidList = false;
                }
                else
                    Global.SysUtil.Log("Skipped List: " + ProcessingList.Name);
            }

            // Log error if key specialized lists missing
            for (int i = 0; i < TotalSpecializedLists; i++)
            {
                if (HasSpecializedLists[i] == 0)
                    Global.InputErrors.Add(new Error(student, null, null, Error.Owner.admin, String.Format("Missing {0} List", ApplicationListsName[i])));
                else if(HasSpecializedLists[i] > 1)
                    Global.InputErrors.Add(new Error(student, null, null, Error.Owner.admin, String.Format("More than one {0} List found", ApplicationListsName[i])));
            }
        }

        public void ReadTaskCards(Manatee.Trello.List list, SOF_Application student, ref SOF_WorkList schoolList)
        {
            Task task = null;
            bool ValidCard = false;

            foreach (var card in list.Cards)
            {
                // Try to validate list name
                SetProcessingContext(card);

                // Detech Comment Card that begins wtih #
                if (card.Name.StartsWith("#"))
                    continue;

                task = null;
                ValidCard = false;

                if (schoolList.ListType == "SL")
                    ValidCard = card.Name.Contains("Exploratory School List") ? true : false;
                else if (schoolList.ListType == "RL")
                    ValidCard = card.Name.Contains("Rec 1") || card.Name.Contains("Rec 2") || card.Name.Contains("Rec 3") ? true : false;
                else if (schoolList.ListType == "T")
                    ValidCard = card.Name.Contains("Application Materials Mailing") ? true : false;
                else if (schoolList.ListType == "SS")
                    ValidCard = card.Name.Contains("Sending Scores") ? true : false;
                else if (schoolList.ListType == "LI")
                    ValidCard = card.Name.Contains("Application Status check") ? true : false;
                /* Not using AF in 2018-19
                else if (schoolList.ListType == "AF")
                    ValidCard = card.Name.Contains("Provide CA fill in instruction and review") ||
                                card.Name.Contains("CA Forms") ||
                                card.Name.Contains("UC Application form") ? true : false;
                */
                else if (schoolList.ListType == "CF")
                    ValidCard = card.Name.Contains("COF and Bank Statement") ||
                                card.Name.Contains("Financial Aid") ? true : false;
                /*  Not using V in 2018-19
                else if (schoolList.ListType == "V")
                    ValidCard = card.Name.Contains("F1 Visa") ? true : false;
                */
                else if (schoolList.ListType == "School")
                    ValidCard = card.Name.Contains("Final Result and PDF") ? true : false;
                else
                {
                }

                if (ValidCard)
                {
                    task = new Task(card.Id);
                    task.SetCard(card.Name, card.Url);
                    ReadChecklists(card, ref task);
                    schoolList.Tasks.Add(task);
                }

                task = null;
            }
        }

        public void ReadEssayCards(Manatee.Trello.List list, SOF_Application student, ref SOF_WorkList schoolList)
        {
            Essay essay = null;
            bool ValidCard = false;

            // Setup parser
            Regex CEFEssayRegex = new Regex(CEFCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
            Regex PSEssayRegex = new Regex(PSCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
            Regex HALEssayRegex = new Regex(HALCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
            Regex ASEssayRegex = new Regex(SupplimentEssayCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));

            foreach (var card in list.Cards)
            {
                // Try to validate list name
                SetProcessingContext(card);

                // Detech Comment Card that begins wtih #
                if (card.Name.StartsWith("#"))
                {
                    // Discover mis-use of # tag
                    // if (card.Name != "# PS-T1-D1 Basketball" && card.Name != "# PS-T2-D1 Bassoon" && card.Name != "# CEF-D1")
                    //    Global.InputErrors.Add(new Error(ProcessingBoard, ProcessingList, ProcessingCard, String.Format("Misuse of &num; tag")));

                    continue;
                }

                try
                {
                    Match m;

                    ValidCard = false;

                    if (schoolList != null)
                    {
                        // Find CEF essay cards
                        if (schoolList.ListType == "CEF")
                        {
                            m = CEFEssayRegex.Match(card.Name);
                            if (m.Success)
                            {
                                int Essay = -1;
                                bool BrainStorm = false;
                                int Draft = -1;
                                bool Final = false;

                                essay = new Essay(card.Id);
                                if (m.Groups["Number"].Value == "CEF")
                                {
                                    Essay = 1;
                                    if (m.Groups["Draft"].Value != "")
                                    {
                                        Draft = Convert.ToInt32(m.Groups["Draft"].Value);
                                        Final = (m.Groups["Final"].Value != "") ? true : false;
                                    }
                                    else
                                    {
                                        Draft = -1;
                                        Global.InputErrors.Add(new Error(student, schoolList, essay, Error.Owner.ac, String.Format("CEF should have draft #")));
                                    }
                                }
                                else if (m.Groups["Number"].Value == "CER")
                                {
                                    Essay = 2;
                                    Draft = 1;
                                    Final = true;
                                }

                                BrainStorm = m.Groups["Brainstorm"].Value != "" ? true : false;

                                essay.SetCard(card.Name, card.Url, Essay, BrainStorm, Draft,
                                          (m.Groups["Revision"].Value != "") ? (Convert.ToInt32(m.Groups["Revision"].Value)) : -1,
                                          Final);
                            }
                            ValidCard = m.Success;
                        }
                        // Find PS essay cards
                        else if (schoolList.ListType == "PS")
                        {
                            m = PSEssayRegex.Match(card.Name);
                            if (m.Success)
                            {
                                essay = new Essay(card.Id);

                                essay.SetCard(card.Name, card.Url, Convert.ToInt32(m.Groups["Number"].Value),
                                              m.Groups["Brainstorm"].Value != "" ? true : false,
                                              Convert.ToInt32(m.Groups["Draft"].Value),
                                              (m.Groups["Revision"].Value != "") ? (Convert.ToInt32(m.Groups["Revision"].Value)) : -1,
                                              (m.Groups["Final"].Value != "") ? true : false);
                            }
                            ValidCard = m.Success;
                        }
                        // Find HAL essay cards
                        else if (schoolList.ListType == "HAL")
                        {
                            m = HALEssayRegex.Match(card.Name);
                            if (m.Success)
                            {
                                essay = new Essay(card.Id);
                                essay.SetCard(card.Name, card.Url, m.Groups["Number"].Value == "MAJ" ? 1 : 2,
                                              m.Groups["Brainstorm"].Value != "" ? true : false,
                                              Convert.ToInt32(m.Groups["Draft"].Value),
                                              (m.Groups["Revision"].Value != "") ? (Convert.ToInt32(m.Groups["Revision"].Value)) : -1,
                                              (m.Groups["Final"].Value != "") ? true : false);
                            }
                            ValidCard = m.Success;
                        }
                        // Find School Suppliment essay cards
                        else
                        {
                            m = ASEssayRegex.Match(card.Name);
                            if (m.Success)
                            {
                                essay = new Essay(card.Id);
                                essay.SetCard(card.Name, card.Url, Convert.ToInt32(m.Groups["Number"].Value),
                                              m.Groups["Brainstorm"].Value != "" ? true : false,
                                              Convert.ToInt32(m.Groups["Draft"].Value),
                                              (m.Groups["Revision"].Value != "") ? (Convert.ToInt32(m.Groups["Revision"].Value)) : -1,
                                              (m.Groups["Final"].Value != "") ? true : false);
                            }
                            ValidCard = m.Success;
                        }
                    }
                }
                catch (RegexMatchTimeoutException)
                {
                    Global.SysUtil.Log("The matching operation timed out.");
                    ValidCard = false;
                }

                if (ValidCard)
                {
                    // Parse Due Date
                    if (card.DueDate.HasValue)
                    {
                        essay.hasDueDate = true;
                        essay.DueDate = card.DueDate.Value;
                    }

                    ReadAttachments(card, essay.Attachments);

                    // Set Cards in List
                    schoolList.Essays.Add(essay);
                    essay = null;
                }
            }
        }

        public void ReadChecklists(Manatee.Trello.Card card, ref Task listCard)
        {            
            if (card.CheckLists.Count() > 0)
            {
                foreach (var checklist in card.CheckLists)
                {
                    if (checklist.Name == "AC Checklist" ||
                        checklist.Name == "EC Checklist" ||
                        checklist.Name == "Student Checklist" ||
                        checklist.Name == "Final Result Update to CRM" ||
                        checklist.Name == "EA/ED" ||
                        checklist.Name == "RD")
                    {
                        Checklist cl = new Checklist();
                        cl.ChecklistName = checklist.Name;
                        ReadChecklistItems(checklist, ref cl);
                        listCard.TaskLists.Add(cl);

                        Global.SysUtil.Log(String.Format("Checklist: {0}, {1} tasks", cl.ChecklistName, cl.Items.Count()));
                    }
                }
            }
        }

        public void ReadChecklistItems(Manatee.Trello.CheckList checklist, ref Checklist cl)
        {
            string CheckItemPattern = @"^T(?<TaskNumber>\d+)\s?[:：]";
            Regex CheckItemRegex = new Regex(CheckItemPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));

            if (checklist.CheckItems.Count() > 0)
            {
                foreach (var checkitem in checklist.CheckItems)
                {
                    Match m = CheckItemRegex.Match(checkitem.Name);
                    if (m.Success)
                    {
                        CheckItem ci = new CheckItem();
                        ci.CheckItemName = checkitem.Name;

                        if (m.Groups["TaskNumber"].Value != "")
                            ci.Number = Int32.Parse(m.Groups["TaskNumber"].Value);

                        ci.State = checkitem.State == CheckItemState.Complete ? true : false;

                        cl.Items.Add(ci);
                    }
                }
            }
        }
/*
        public void RegulationExpressionTest()
        {
            Regex r = null;
            Match m;
            string test;

            string MonthlyPlanCardPattern = @"(?i:P)\s*[:-]\s*(?<Year>\d+)\s*[/.-]\s*(?<Month>\d+)";
            string MeetingRecapCardPattern = @"(?i:R)\s*[:-]\s*(?<Year>\d+)\s*[/.-]\s*(?<Month>\d+)\s*[/.-]\s*(?<Day>\d+)";

            Console.Write("1. Board, 2. School List, 3. CEF Card, 4. PS Card, 5. HAL Card, 6. Essay, 7. Attachment, 8. Guidance Plan, 9: Guidance Recap: ");
            int selection = Int32.Parse(Console.ReadLine());

            switch (selection)
            {
                case 1:
                    r = new Regex(BoardNamePattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                case 2:
                    r = new Regex(ApplicationSchoolListPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                case 3:
                    r = new Regex(CEFCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                case 4:
                    r = new Regex(PSCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                case 5:
                    r = new Regex(HALCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                case 6:
                    r = new Regex(SupplimentEssayCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                case 8:
                    r = new Regex(MonthlyPlanCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                case 9:
                    r = new Regex(MeetingRecapCardPattern, RegexOptions.None, TimeSpan.FromMilliseconds(150));
                    break;
                default:
                    break;
            }

            if (r != null || selection == 7)
            {
                while (true)
                {
                    int en = -1;
                    int dn = -1;
                    int rn = -1;
                    test = Console.ReadLine();

                    if (test == "end")
                        break;

                    if (selection == 7)
                    {
                        if (ParseAttachmentName(test, ref en, ref dn, ref rn))
                            Console.WriteLine("Success: Essay {0}, Draft {1}. Revision {2}", en, dn, rn);
                        else
                            Console.WriteLine("Failed");
                    }
                    else if (selection == 8)
                    {
                        m = r.Match(test);
                        if (m.Success)
                            Console.WriteLine("Monthly Plan: {0}-{1}", Convert.ToInt32(m.Groups["Year"].Value), Convert.ToInt32(m.Groups["Month"].Value));
                        else
                            Console.WriteLine("Failed");
                    }
                    else if (selection == 9)
                    {
                        m = r.Match(test);
                        if (m.Success)
                            Console.WriteLine("Meeting Recap: {0}-{1}-{2}", Convert.ToInt32(m.Groups["Year"].Value), Convert.ToInt32(m.Groups["Month"].Value), Convert.ToInt32(m.Groups["Day"].Value));
                        else
                            Console.WriteLine("Failed");
                    }
                    else
                    {
                        m = r.Match(test);
                        if (m.Success)
                            Console.WriteLine("Success: Number {0}, Draft {1}. Revision {2}, Final {3}", m.Groups["Number"].Value, m.Groups["Draft"].Value, m.Groups["Revision"].Value, (m.Groups["Final"].Value != "") ? 1 : 0);
                        else
                            Console.WriteLine("Failed");
                    }
                }
            }
        }
*/
    }
}
